package cn.com.huzo.webapp.controller.trade;

import cn.com.huzo.model.entities.trade.CommercialInvoiceHeaderEntity;
import cn.com.huzo.model.entities.trade.CommercialInvoiceLinesEntity;
import cn.com.huzo.service.trade.CommercialInvoiceHeaderManager;
import cn.com.huzo.service.trade.CommercialInvoiceLinesManager;
import huzofw.common.web.controller.BaseFormController;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 销售发票画面控制器
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/trade/commercial_invoice")
public class CommercialInvoiceController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "CommercialInvoiceHeaderManager")
    private CommercialInvoiceHeaderManager _manager_header;

    @Resource(name = "CommercialInvoiceLinesManager")
    private CommercialInvoiceLinesManager _manager_lines;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到销售发票一览画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("trade.commercial_invoice.list.def");
        return view;
    }

    /**
     * 跳转销售发票编辑画面
     * <p>
     * @param request 上下文请求
     * @param action 操作标志 1;增加 2：编辑
     * @param headerId 销售发票标识
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/{action}/{headerId}/edit.html")
    public ModelAndView redirectEditWnd(HttpServletRequest request, @PathVariable(value = "action") int action,
            @PathVariable(value = "headerId") Long headerId) throws Exception {
        ModelAndView view = new ModelAndView();
        view.addObject("action", action);
        view.addObject("headerId", headerId.toString());
        view.setViewName("trade.commercial_invoice.edit.def");
        return view;
    }

    /**
     * 销售发票明细增加画面
     * <p>
     * @return
     */
    @RequestMapping("/newLinesWizard.html")
    public ModelAndView redirectNewLinesWizardWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("01_trade/commercialInvoice_newLinesWizard");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="主表处理部分">
    /**
     * 根据ID取得销售发票数据。
     *
     * @param headerId 销售发票ID
     * @return 销售发票数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody final Long headerId) throws Exception {
        return _manager_header.fetchByHeaderKey(headerId);
    }

    /**
     * 根据条件取得销售发票信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 分页数据
     * @throws Exception
     */
    @RequestMapping(value = "/header/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchForPage(criterion);
    }

    /**
     * 保存销售发票信息数据。
     * <p>
     * @param instance 要保存的数据
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "/header/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody final CommercialInvoiceHeaderEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getHeaderId() > 0) { //编辑
            rowsAffected = (long) _manager_header.update(instance);
        } else {//新增
            rowsAffected = _manager_header.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 删除销售发票信息。
     *
     * @param criterion 要删除的数据
     * @return 影响行数
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.delete(criterion);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="明细处理部分">
    /**
     * 搜索销售合同和发货单列表数据。
     *
     * @param criterion 要搜索的条件
     * @return 销售合同和发货单列表明细数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/wizard/search.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchWizardSearchResult(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.fetchWizardSearchResult(criterion);
    }

    /**
     * 根据所选择的销售合同和发货单，提取搭配后的数据。
     *
     * @param criterion 选择的搭配条件
     * @return 销售发票明细数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/wizard/fetchByWizardResult.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<CommercialInvoiceLinesEntity> fetchByWizardResult(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.fetchByWizardResult(criterion);
    }

    /**
     * 增加销售发票明细数据。
     *
     * @param criterion 要增加或更新的数据
     * @return 明细数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> savelines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.batchUpdate(criterion);
    }

    /**
     * 批量删除销售发票明细数据。
     *
     * @param itemlist 要删除的数据
     * @return 影响Ｆ的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/batch_delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchDeleteLines(@RequestBody final List<CommercialInvoiceLinesEntity> itemlist) throws Exception {
        return _manager_lines.batchDelete(itemlist);
    }

    /**
     * 删除单条销售发票明细数据。
     *
     * @param criterion 要删除的数据
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteOne(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.delete(criterion);
    }
    // </editor-fold>
}
